package io.renren.modules.creditWarning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.creditWarning.dao.ActivityDao;
import io.renren.modules.creditWarning.dto.ActivityDTO;
import io.renren.modules.creditWarning.entity.ActivityEntity;
import io.renren.modules.creditWarning.service.ActivityService;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 活动信息表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-05-12
 */
@Service
public class ActivityServiceImpl extends CrudServiceImpl<ActivityDao, ActivityEntity, ActivityDTO> implements ActivityService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<ActivityEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String title = (String)params.get("title");
        String status = (String)params.get("status");

        QueryWrapper<ActivityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(title), "id", title);
        wrapper.eq(StringUtils.isNotBlank(status), "status", status);

        return wrapper;
    }


    @Override
    public PageData<ActivityDTO> getPage(Map<String, Object> params) {
        PageData<ActivityDTO> page = this.page(params);
        page.getList().forEach(e -> {
            SysUserDTO creatorDto = sysUserService.get(e.getCreator());
            e.setCreatorName(creatorDto == null ? "" : creatorDto.getUsername());
            SysUserDTO updaterDto = sysUserService.get(e.getUpdater());
            e.setUpdaterName(creatorDto == null ? "" : updaterDto.getUsername());
        });
        return page;
    }

}